<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>tag</title>
<link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../../index.html" title="Chapter 1. Geometry">
<link rel="up" href="../core.html" title="Core Metafunctions">
<link rel="prev" href="ring_type.html" title="ring_type">
<link rel="next" href="tag_cast.html" title="tag_cast">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="ring_type.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../core.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="tag_cast.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="geometry.reference.core.tag"></a><a class="link" href="tag.html" title="tag">tag</a>
</h4></div></div></div>
<p>
          <a class="indexterm" name="idm70901"></a>
Metafunction defining <span class="bold"><strong>type</strong></span> as the tag
          of the specified geometry type.
        </p>
<h6>
<a name="geometry.reference.core.tag.h0"></a>
          <span class="phrase"><a name="geometry.reference.core.tag.description"></a></span><a class="link" href="tag.html#geometry.reference.core.tag.description">Description</a>
        </h6>
<p>
          With Boost.Geometry, tags are the driving force of the tag dispatching
          mechanism. The tag metafunction is therefore used in every free function.
        </p>
<h6>
<a name="geometry.reference.core.tag.h1"></a>
          <span class="phrase"><a name="geometry.reference.core.tag.synopsis"></a></span><a class="link" href="tag.html#geometry.reference.core.tag.synopsis">Synopsis</a>
        </h6>
<p>
</p>
<pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Geometry</span><span class="special">&gt;</span>
<span class="keyword">struct</span> <span class="identifier">tag</span>
<span class="special">{</span>
  <span class="comment">// ...</span>
<span class="special">};</span>
</pre>
<p>
        </p>
<h6>
<a name="geometry.reference.core.tag.h2"></a>
          <span class="phrase"><a name="geometry.reference.core.tag.template_parameter_s_"></a></span><a class="link" href="tag.html#geometry.reference.core.tag.template_parameter_s_">Template
          parameter(s)</a>
        </h6>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                  <p>
                    Parameter
                  </p>
                </th>
<th>
                  <p>
                    Description
                  </p>
                </th>
</tr></thead>
<tbody><tr>
<td>
                  <p>
                    typename Geometry
                  </p>
                </td>
<td>
                  <p>
                    Any type fulfilling a Geometry Concept
                  </p>
                </td>
</tr></tbody>
</table></div>
<h6>
<a name="geometry.reference.core.tag.h3"></a>
          <span class="phrase"><a name="geometry.reference.core.tag.header"></a></span><a class="link" href="tag.html#geometry.reference.core.tag.header">Header</a>
        </h6>
<p>
          Either
        </p>
<p>
          <code class="computeroutput"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">geometry</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span></code>
        </p>
<p>
          Or
        </p>
<p>
          <code class="computeroutput"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">geometry</span><span class="special">/</span><span class="identifier">core</span><span class="special">/</span><span class="identifier">tag</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span></code>
        </p>
<h6>
<a name="geometry.reference.core.tag.h4"></a>
          <span class="phrase"><a name="geometry.reference.core.tag.metafunction_result_type"></a></span><a class="link" href="tag.html#geometry.reference.core.tag.metafunction_result_type">Metafunction
          result type</a>
        </h6>
<p>
          The metafunction tag defines <span class="bold"><strong>type</strong></span> as one
          of the following tags:
        </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
              point_tag
            </li>
<li class="listitem">
              linestring_tag
            </li>
<li class="listitem">
              polygon_tag
            </li>
<li class="listitem">
              multi_point_tag
            </li>
<li class="listitem">
              multi_linestring_tag
            </li>
<li class="listitem">
              multi_polygon_tag
            </li>
<li class="listitem">
              box_tag
            </li>
<li class="listitem">
              segment_tag
            </li>
<li class="listitem">
              ring_tag
            </li>
</ul></div>
<h6>
<a name="geometry.reference.core.tag.h5"></a>
          <span class="phrase"><a name="geometry.reference.core.tag.complexity"></a></span><a class="link" href="tag.html#geometry.reference.core.tag.complexity">Complexity</a>
        </h6>
<p>
          Compile time
        </p>
<h6>
<a name="geometry.reference.core.tag.h6"></a>
          <span class="phrase"><a name="geometry.reference.core.tag.example"></a></span><a class="link" href="tag.html#geometry.reference.core.tag.example">Example</a>
        </h6>
<p>
          Shows how tag dispatching essentially works in Boost.Geometry
        </p>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>

<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">geometry</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">geometry</span><span class="special">/</span><span class="identifier">geometries</span><span class="special">/</span><span class="identifier">polygon</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">geometry</span><span class="special">/</span><span class="identifier">geometries</span><span class="special">/</span><span class="identifier">multi_polygon</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">geometry</span><span class="special">/</span><span class="identifier">geometries</span><span class="special">/</span><span class="identifier">adapted</span><span class="special">/</span><span class="identifier">boost_tuple</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">assign</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span> <a class="co" name="geometry.reference.core.tag.c0" href="tag.html#geometry.reference.core.tag.c1"><img src="../../../../../../../doc/src/images/callouts/1.png" alt="1" border="0"></a>

<span class="identifier">BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS</span><span class="special">(</span><span class="identifier">cs</span><span class="special">::</span><span class="identifier">cartesian</span><span class="special">)</span>

<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Tag</span><span class="special">&gt;</span> <span class="keyword">struct</span> <span class="identifier">dispatch</span> <span class="special">{};</span>

<span class="comment">// Specialization for points</span>
<span class="keyword">template</span> <span class="special">&lt;&gt;</span> <span class="keyword">struct</span> <span class="identifier">dispatch</span><span class="special">&lt;</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">geometry</span><span class="special">::</span><span class="identifier">point_tag</span><span class="special">&gt;</span>
<span class="special">{</span>
    <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Point</span><span class="special">&gt;</span>
    <span class="keyword">static</span> <span class="keyword">inline</span> <span class="keyword">void</span> <span class="identifier">apply</span><span class="special">(</span><span class="identifier">Point</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">p</span><span class="special">)</span>
    <span class="special">{</span>
        <span class="comment">// Use the Boost.Geometry free function "get"</span>
        <span class="comment">// working on all supported point types</span>
        <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Hello POINT, you are located at: "</span>
            <span class="special">&lt;&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">geometry</span><span class="special">::</span><span class="identifier">get</span><span class="special">&lt;</span><span class="number">0</span><span class="special">&gt;(</span><span class="identifier">p</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="string">", "</span>
            <span class="special">&lt;&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">geometry</span><span class="special">::</span><span class="identifier">get</span><span class="special">&lt;</span><span class="number">1</span><span class="special">&gt;(</span><span class="identifier">p</span><span class="special">)</span>
            <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
    <span class="special">}</span>
<span class="special">};</span>

<span class="comment">// Specialization for polygons</span>
<span class="keyword">template</span> <span class="special">&lt;&gt;</span> <span class="keyword">struct</span> <span class="identifier">dispatch</span><span class="special">&lt;</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">geometry</span><span class="special">::</span><span class="identifier">polygon_tag</span><span class="special">&gt;</span>
<span class="special">{</span>
    <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Polygon</span><span class="special">&gt;</span>
    <span class="keyword">static</span> <span class="keyword">inline</span> <span class="keyword">void</span> <span class="identifier">apply</span><span class="special">(</span><span class="identifier">Polygon</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">p</span><span class="special">)</span>
    <span class="special">{</span>
        <span class="comment">// Use the Boost.Geometry manipulator "dsv"</span>
        <span class="comment">// working on all supported geometries</span>
        <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Hello POLYGON, you look like: "</span>
            <span class="special">&lt;&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">geometry</span><span class="special">::</span><span class="identifier">dsv</span><span class="special">(</span><span class="identifier">p</span><span class="special">)</span>
            <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
    <span class="special">}</span>
<span class="special">};</span>

<span class="comment">// Specialization for multipolygons</span>
<span class="keyword">template</span> <span class="special">&lt;&gt;</span> <span class="keyword">struct</span> <span class="identifier">dispatch</span><span class="special">&lt;</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">geometry</span><span class="special">::</span><span class="identifier">multi_polygon_tag</span><span class="special">&gt;</span>
<span class="special">{</span>
    <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">MultiPolygon</span><span class="special">&gt;</span>
    <span class="keyword">static</span> <span class="keyword">inline</span> <span class="keyword">void</span> <span class="identifier">apply</span><span class="special">(</span><span class="identifier">MultiPolygon</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">m</span><span class="special">)</span>
    <span class="special">{</span>
        <span class="comment">// Use the Boost.Range free function "size" because all</span>
        <span class="comment">// multigeometries comply to Boost.Range</span>
        <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Hello MULTIPOLYGON, you contain: "</span>
            <span class="special">&lt;&lt;</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">size</span><span class="special">(</span><span class="identifier">m</span><span class="special">)</span> <span class="special">&lt;&lt;</span> <span class="string">" polygon(s)"</span>
            <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
    <span class="special">}</span>
<span class="special">};</span>

<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Geometry</span><span class="special">&gt;</span>
<span class="keyword">inline</span> <span class="keyword">void</span> <span class="identifier">hello</span><span class="special">(</span><span class="identifier">Geometry</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">geometry</span><span class="special">)</span>
<span class="special">{</span>
    <span class="comment">// Call the metafunction "tag" to dispatch, and call method (here "apply")</span>
    <span class="identifier">dispatch</span>
        <span class="special">&lt;</span>
            <span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">geometry</span><span class="special">::</span><span class="identifier">tag</span><span class="special">&lt;</span><span class="identifier">Geometry</span><span class="special">&gt;::</span><span class="identifier">type</span>
        <span class="special">&gt;::</span><span class="identifier">apply</span><span class="special">(</span><span class="identifier">geometry</span><span class="special">);</span>
<span class="special">}</span>

<span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span>
<span class="special">{</span>
    <span class="comment">// Define polygon type (here: based on a Boost.Tuple)</span>
    <span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">geometry</span><span class="special">::</span><span class="identifier">model</span><span class="special">::</span><span class="identifier">polygon</span><span class="special">&lt;</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">tuple</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">,</span> <span class="keyword">int</span><span class="special">&gt;</span> <span class="special">&gt;</span> <span class="identifier">polygon_type</span><span class="special">;</span>

    <span class="comment">// Declare and fill a polygon and a multipolygon</span>
    <span class="identifier">polygon_type</span> <span class="identifier">poly</span><span class="special">;</span>
    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">geometry</span><span class="special">::</span><span class="identifier">exterior_ring</span><span class="special">(</span><span class="identifier">poly</span><span class="special">)</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">assign</span><span class="special">::</span><span class="identifier">tuple_list_of</span><span class="special">(</span><span class="number">0</span><span class="special">,</span> <span class="number">0</span><span class="special">)(</span><span class="number">0</span><span class="special">,</span> <span class="number">10</span><span class="special">)(</span><span class="number">10</span><span class="special">,</span> <span class="number">5</span><span class="special">)(</span><span class="number">0</span><span class="special">,</span> <span class="number">0</span><span class="special">);</span>

    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">geometry</span><span class="special">::</span><span class="identifier">model</span><span class="special">::</span><span class="identifier">multi_polygon</span><span class="special">&lt;</span><span class="identifier">polygon_type</span><span class="special">&gt;</span> <span class="identifier">multi</span><span class="special">;</span>
    <span class="identifier">multi</span><span class="special">.</span><span class="identifier">push_back</span><span class="special">(</span><span class="identifier">poly</span><span class="special">);</span>

    <span class="comment">// Call "hello" for point, polygon, multipolygon</span>
    <span class="identifier">hello</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">make_tuple</span><span class="special">(</span><span class="number">2</span><span class="special">,</span> <span class="number">3</span><span class="special">));</span>
    <span class="identifier">hello</span><span class="special">(</span><span class="identifier">poly</span><span class="special">);</span>
    <span class="identifier">hello</span><span class="special">(</span><span class="identifier">multi</span><span class="special">);</span>

    <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
        </p>
<div class="calloutlist"><table border="0" summary="Callout list"><tr>
<td width="5%" valign="top" align="left"><p><a name="geometry.reference.core.tag.c1"></a><a href="#geometry.reference.core.tag.c0"><img src="../../../../../../../doc/src/images/callouts/1.png" alt="1" border="0"></a> </p></td>
<td valign="top" align="left"><p>
              At the end to avoid conflicts with Boost.QVM
            </p></td>
</tr></table></div>
<p>
          Output:
        </p>
<pre class="programlisting">Hello POINT, you are located at: 2, 3
Hello POLYGON, you look like: (((0, 0), (0, 10), (10, 5), (0, 0)))
Hello MULTIPOLYGON, you contain: 1 polygon(s)
</pre>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2009-2021 Barend Gehrels,
      Bruno Lalande, Mateusz Loskot, Adam Wulkiewicz, Oracle and/or its affiliates<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="ring_type.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../core.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="tag_cast.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
